package com.spit.hkpost.ui.newfrg

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ImageButton
import android.widget.Spinner
import androidx.appcompat.widget.AppCompatEditText
import androidx.fragment.app.activityViewModels
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.StringUtils
import com.flyco.roundview.RoundTextView
import com.spit.hkpost.R
import com.spit.hkpost.base.BaseFullBottomSheetFragment
import com.spit.hkpost.bean.AppRoomDataBase
import com.spit.hkpost.ext.showToast
import com.spit.hkpost.util.CacheUtil
import com.spit.hkpost.viewmodel.RfidModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


/**
 * User: Nike
 *  2023/12/19 16:14
 */
class ConfigBottomFrg() : BaseFullBottomSheetFragment(), OnClickListener {

    val sp_session_data = arrayOf("S0", "S1", "S2", "S3")
    val sp_flag_data = arrayOf("A", "B", "AB")
    val sp_language_data = arrayOf("简体", "繁體", "English")

    var powerSelected: Int = 0
    var sessionSelected: Int = 0
    var flagSelected: Int = 0
    var languageSelected: Int = 0

    lateinit var etRssi: AppCompatEditText
    lateinit var etEaf: AppCompatEditText
    lateinit var etRfidCode: AppCompatEditText
    lateinit var etRecipient: AppCompatEditText
    lateinit var etStrength: AppCompatEditText
    lateinit var etPrefix: AppCompatEditText

    lateinit var spPower: Spinner
    lateinit var spSession: Spinner
    lateinit var spLanguage: Spinner

    val configDao = AppRoomDataBase.get().getConfigDao()

    val rfidModel: RfidModel by activityViewModels()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val rootView = inflater.inflate(R.layout.b_cofig, container, false)
        rootView.findViewById<ImageButton>(R.id.iv_cancel).setOnClickListener(this)
        rootView.findViewById<RoundTextView>(R.id.btn_commit).setOnClickListener(this)
        rootView.findViewById<RoundTextView>(R.id.bt_prefix).setOnClickListener(this)
        spPower = rootView.findViewById<Spinner>(R.id.sp_power)
        var sp_power_data = ArrayList<Int>()
        for (i in 0..32) {
            sp_power_data.add(i)
        }
        val powerAdapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, sp_power_data)
        spPower.adapter = powerAdapter
        spPower.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?,view: View?,position: Int,id: Long) {
                // 当选择项发生变化时触发的逻辑
                powerSelected = position
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                // 当没有选择项时触发的逻辑
            }
        }

        val sp_session_data = arrayOf(getString(R.string.single_label), getString(R.string.double_label))
        spSession = rootView.findViewById<Spinner>(R.id.sp_session)
        val sessionAdapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, sp_session_data)
        spSession.adapter = sessionAdapter
        spSession.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?,view: View?,position: Int,id: Long) {
                // 当选择项发生变化时触发的逻辑
                sessionSelected = position
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                // 当没有选择项时触发的逻辑
            }
        }

        /*val spFlag = rootView.findViewById<Spinner>(R.id.sp_flag)
        val flagAdapter =
            ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, sp_flag_data)
        spFlag.adapter = flagAdapter
        spFlag.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?,view: View?,position: Int,id: Long) {
                // 当选择项发生变化时触发的逻辑
                flagSelected = position
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                // 当没有选择项时触发的逻辑
            }
        }*/

        spLanguage = rootView.findViewById<Spinner>(R.id.sp_language)
        val languageAdapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, sp_language_data)
        spLanguage.adapter = languageAdapter
        spLanguage.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                // 当选择项发生变化时触发的逻辑
                languageSelected = position
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                // 当没有选择项时触发的逻辑
            }
        }

        etRssi = rootView.findViewById(R.id.et_rssi)
        etEaf = rootView.findViewById(R.id.et_eaf)
        etRfidCode = rootView.findViewById(R.id.et_rfid_code)
        etRecipient = rootView.findViewById(R.id.et_recipient)
        etStrength = rootView.findViewById(R.id.et_strength)
        etPrefix = rootView.findViewById(R.id.et_prefix)

        onUploadUi()
        return rootView
    }

    private fun onUploadUi() {
        GlobalScope.launch {
            var bean = configDao.findFirst()
            withContext(Dispatchers.Main) {
                spPower.setSelection(bean.power)
                spSession.setSelection(bean.session)
//                spFlag.setSelection(bean.flag)
                etRssi.setText("${bean.rssi}")
                etEaf.setText("${bean.eaf}")
                etRfidCode.setText("${bean.rfidCode}")
//                etRecipient.setText("${bean.recipient}")
                etStrength.setText("${bean.strength}")
                etPrefix.setText("${bean.prefix}")
                spLanguage.setSelection(CacheUtil.getLanguage())
            }
        }
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.iv_cancel -> {
                dismiss()
            }
            R.id.bt_prefix ->{
                GlobalScope.launch {
                    var bean = configDao.findFirst()
                    bean.prefix = etPrefix.text.toString()
                    configDao.insertOrUpdate(bean)
                    withContext(Dispatchers.Main) {
                        showToast(requireContext().getString(R.string.text4))
                    }
                }
            }

            R.id.btn_commit -> {
                GlobalScope.launch {
                    var bean = configDao.findFirst()
                    bean.power = powerSelected
                    bean.session = sessionSelected
//                    bean.flag = flagSelected
                    bean.eaf = etEaf.text.toString().toDouble()
                    bean.rssi = etRssi.text.toString().toInt()
                    bean.rfidCode = etRfidCode.text.toString()
//                    bean.recipient = etRecipient.text.toString()
                    bean.strength = etStrength.text.toString().toInt()
//                    bean.prefix = etPrefix.text.toString()

                    if (!StringUtils.isEmpty(bean.recipient) && !RegexUtils.isEmail(bean.recipient)){
//                        showToast(getString(R.string.email_format_error))
//                        return@launch
                    }
                    configDao.insertOrUpdate(bean)

                    withContext(Dispatchers.Main) {
                        if (languageSelected != CacheUtil.getLanguage()){
                            rfidModel.language.value = languageSelected
                        }
//                        showToast("save successful")
                        dismiss()
                    }
                }
            }
        }
    }

    override fun dismiss() {
        super.dismiss()
        rfidModel.language.value = -1
    }

}


